package cn.edu.imau.zy.student.service.impl;

import java.time.LocalDate;
import java.util.*;

import cn.edu.imau.zy.common.core.entity.student.StLeave;
import cn.edu.imau.zy.common.utils.DateUtils;
import cn.edu.imau.zy.student.dto.LocalDateRange;
import cn.edu.imau.zy.student.service.IStLeaveService;
import cn.edu.imau.zy.student.mapper.StLeaveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.edu.imau.zy.common.core.text.Convert;

/**
 * 学生请假Service业务层处理
 *
 * @author wyz
 * @date 2020-07-01
 */
@Service
public class StLeaveServiceImpl implements IStLeaveService {
    @Autowired
    private StLeaveMapper stLeaveMapper;

    /**
     * 查询学生请假
     *
     * @param id 学生请假ID
     * @return 学生请假
     */
    @Override
    public StLeave selectStLeaveById(Long id) {
        return stLeaveMapper.selectStLeaveById(id);
    }

    /**
     * 查询学生请假列表
     *
     * @param stLeave 学生请假
     * @return 学生请假
     */
    @Override
    public List<StLeave> selectStLeaveList(StLeave stLeave) {
        return stLeaveMapper.selectStLeaveList(stLeave);
    }

    /**
     * 新增学生请假
     *
     * @param stLeave 学生请假
     * @return 结果
     */
    @Override
    public int insertStLeave(StLeave stLeave) {
        stLeave.setCreateTime(DateUtils.getNowDate());
        return stLeaveMapper.insertStLeave(stLeave);
    }

    /**
     * 修改学生请假
     *
     * @param stLeave 学生请假
     * @return 结果
     */
    @Override
    public int updateStLeave(StLeave stLeave) {
        stLeave.setUpdateTime(DateUtils.getNowDate());
        System.out.println("==============================");
        System.out.println("stLeave:"+stLeave);
        return stLeaveMapper.updateStLeave(stLeave);
    }

    /**
     * 删除学生请假对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStLeaveByIds(String ids) {
        return stLeaveMapper.deleteStLeaveByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生请假信息
     *
     * @param id 学生请假ID
     * @return 结果
     */
    @Override
    public int deleteStLeaveById(Long id) {
        return stLeaveMapper.deleteStLeaveById(id);
    }

    @Override
    public List<StLeave> selectStLeaveListByTeacherId(String teacherId) {
        return stLeaveMapper.selectStLeaveListByTeacherId(teacherId);
    }

    @Override
    public int checkHave(StLeave stLeave) {
        return stLeaveMapper.checkHave(stLeave);
    }

    @Override
    public Map<String, Object> monthCount(Map<String, Object> params) {
        LocalDate date = LocalDate.of((int) params.get("year"), (int) params.get("month"), 1);
        params.put("firstDate", date);
        params.put("lastDate", date.withDayOfMonth(date.lengthOfMonth()));
        return stLeaveMapper.monthCount(params);
    }

    @Override
    public HashSet<LocalDate> monthLeaveDay(Map<String, Object> params) {
        LocalDate firstDate = LocalDate.of((int) params.get("year"), (int) params.get("month"), 1);
        LocalDate lastDate = firstDate.withDayOfMonth(firstDate.lengthOfMonth());
        params.put("firstDate", firstDate);
        params.put("lastDate", lastDate);
        List<LocalDateRange> list = stLeaveMapper.monthLeaveDay(params);
        HashSet<LocalDate> res = new HashSet<>();
        for (LocalDateRange item : list) {
            LocalDate start = item.getStartDate();
            LocalDate end = item.getEndDate();
            int s = start.getDayOfMonth(), e = end.getDayOfMonth();
            for (int i = s; i <= e; i++) {
                LocalDate t = start.withDayOfMonth(i);
                if (firstDate.isEqual(t) || lastDate.isEqual(t) || (t.isAfter(firstDate) && t.isBefore(lastDate))) {
                    res.add(start.withDayOfMonth(i));
                }
            }
        }
        return res;
    }

    @Override
    public List<StLeave> dayLeave(Map<String, Object> params) {
        return stLeaveMapper.dayLeave(params);
    }

    @Override
    public List<Map<String, String>> deptLeave() {
        return stLeaveMapper.deptLeave();
    }

    @Override
    public List<Map<String, String>> deptGreadLeave(Map<String, Object> params) {
        return stLeaveMapper.deptGreadLeave(params);
    }

    @Override
    public List<Map<String, String>> classesLeave(Map<String, Object> params) {
        return stLeaveMapper.classesLeave(params);
    }

    @Override
    public List<Map<String, String>> classesDetailLeave(Map<String, Object> params) {
        return stLeaveMapper.classesDetailLeave(params);
    }

    @Override
    public Map<String, Object> countInged(Map<String, Object> params) {
        List<Map<String, Object>> list = stLeaveMapper.countInged(params);
        Map<String, Object> res = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Integer> ingList = new ArrayList<>();
        List<Integer> edList = new ArrayList<>();

        list.forEach(item -> {
            dateList.add(item.get("date").toString());
            ingList.add(Integer.valueOf(item.get("ing").toString()));
            edList.add(Integer.valueOf(item.get("ed").toString()));
        });
        res.put("date", dateList);
        res.put("ing", ingList);
        res.put("ed", edList);
        return res;
    }
}
